package com.hns.can.master.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hns.tool.pub.UtilString;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.master.common.Holiday;
import com.hns.can.master.entity.SysHolidayEntity;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 节假日参数设置 - 业务类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class HolidayParamService extends CommonService {

    /**
     * 节假日参数设置 - 搜索节假日列表
     * @param paraMap
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public Pagination searchHolidayPage(Map<String, Object> paraMap) {
        Pagination page = this.myBatisPage("holidayParam.getHolidayCnt","holidayParam.getHolidayList",paraMap,SysHolidayEntity.class);
        return page;
    }

    /**
     * 节假日参数设置 - 重置节假日为非节假日、工作日
     * @param corpId
     * @param holidayId
     * @param strDate
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public void resetHoliday(String corpId, String holidayId, String strDate) {
        if(PubUtil.isNotEmpty(holidayId)){ // 已经设定过节假日，更新
            SysHolidayEntity entity = commonEntityDao.find(holidayId, SysHolidayEntity.class);
            commonEntityDao.delete(entity);
        }
    }

    /**
     * 节假日参数设置 - 批量重置节假日为非节假日、工作日
     * @param corpId
     * @param holidayIds
     * @param strDates
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public void resetHolidays(String corpId, List<String> holidayIds, List<String> strDates) {
        for(int i = 0; i < holidayIds.size(); i++){
            if(PubUtil.isNotEmpty(holidayIds.get(i))) {
                SysHolidayEntity entity = commonEntityDao.find(holidayIds.get(i), SysHolidayEntity.class);
                commonEntityDao.delete(entity);
            }
        }
    }

    /**
     * 节假日参数设置 - 设定节假日
     * @param corpId
     * @param holidays
     * @param beginDates
     * @param endDates
     * @param workDayInds
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public void setHolidays(String corpId, List<String> holidays,
                List<String> beginDates, List<String> endDates, List<String> workDayInds) throws Exception {
        // 删除t_sys_holiday表中对应corpId、beginDate到endDate之间的记录
        for(int i = 0; i < beginDates.size(); i++){
            String beginDate = beginDates.get(i);
            String endDate = endDates.get(i);
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("corpId", corpId);
            paraMap.put("beginDate", beginDate);
            paraMap.put("endDate", endDate);
            this.sqlMapDao.delete("holidayParam.deleteHolidays", paraMap);
        }
        // 再插入新设定的节假日
        for(int i = 0; i < beginDates.size(); i++){
            String beginDate = beginDates.get(i)+" 00:00:00";
            String endDate = endDates.get(i)+" 23:59:59";
            int days = (int) DateTool.calculateTimeForTimeUnit(beginDate, endDate, DateTool.TIME_UNIT_DAY); // 开始和结束日期之间的天数
            for(int j = 0; j < days; j++){
                String strDate = DateTool.calculateByDate(beginDate, j);
                SysHolidayEntity holiday = new SysHolidayEntity();
                holiday.setHolidayId(UtilString.getUUID());
                holiday.setCorpId(corpId);
                holiday.setHoliday(holidays.get(i));
                holiday.setWorkDayInd(workDayInds.get(i));
                holiday.setStrDate(strDate);
                commonEntityDao.save(holiday);
            }
        }
    }
}